import java.util.Arrays;

/**
 * @author hewei
 * @version 1.0
 * @description: 1105. 填充书架
 * @date 2022/10/30 16:36
 */

public class MinHeightShelves {

    public int minHeightShelves(int[][] books, int shelfWidth) {
        int n = books.length;
        int[] dp = new int[n + 1];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int i = 1; i < n + 1; i++) {
            int maxH = 0, curWidth = 0;
            for (int j = i; j > 0; j--) {
                curWidth += books[j - 1][0];
                if (curWidth > shelfWidth) break;
                maxH = Math.max(maxH, books[j - 1][1]);
                dp[i] = Math.min(dp[i], dp[j - 1] + maxH);
            }
        }
        return dp[n];
    }
}
